#ifndef TASK_TIMER_H
#define TASK_TIMER_H

#include "copy/copyable.h"
#include "Atomic.h"
#include "Timestamp.h"
#include "TimerQueue.h"

class Timer : muduo::noncopyable {
public:
    Timer(TimerCallback cb, Timestamp when, double interval)
            : callback_(std::move(cb)),
              expiration_(when),
              interval_(interval),
              repeat_(interval > 0.0),
              sequence_(s_numCreated_.incrementAndGet()) {}

    void run() const {
        callback_();
    }

    Timestamp expiration() const { return expiration_; }

    bool repeat() const { return repeat_; }

    int64_t sequence() const { return sequence_; }

    void restart(Timestamp now);

    static int64_t numCreated() { return s_numCreated_.get(); }

private:
    /**
     * 回调函数
     */
    const TimerCallback callback_;
    /**
     * 调用时间
     */
    Timestamp expiration_;
    /**
     * 调用间隔
     */
    const double interval_;
    /**
     * 是否重复调用
     */
    const bool repeat_;
    /**
     * 调用顺序
     */
    const int64_t sequence_;
    /**
     * 创建时间
     */
    static AtomicInt64 s_numCreated_;
    // 声明一个int64_t的整形模板
};

#endif //TASK_TIMER_H
